module Lark
  module Apis
    module Im

      # 发送消息
      def send(receive_id_type, payload)
        post "im/v1/messages?receive_id_type=#{receive_id_type}", payload
      end

      # 答复消息
      def reply(message_id, payload)
        post "im/v1/messages/#{message_id}/reply", payload
      end

      # 列出所有聊天室
      def chats(user_access_token:, page_size: nil, page_token: nil)
        get 'im/v1/chats', {
          access_token: user_access_token,
          params: {
            page_size: page_size,
            page_token: page_token,
          }
        }.compact
      end

      # 获取单个聊天室信息
      def chat(chat_id:, user_access_token:)
        get "im/v1/chats/#{chat_id}", {
          access_token: user_access_token,
        }.compact
      end

      # 搜索用户所在的聊天室
      def search(useraccess_token:, query:, page_size: nil, page_token: nil)
        get "im/v1/chats/search", {
          access_token: user_access_token,
          params: {
            query: query,
            page_size: page_size,
            page_token: page_token,
          }
        }.compact
      end

      # 将用户或机器人拉入群聊
      def add_bot_to_chat_by_user(user_access_token:, chat_id:, member_ids:)
        return if member_ids.blank?
        post "im/v1/chats/#{chat_id}/members?member_id_type=app_id", {"id_list": member_ids}, {access_token: user_access_token}
      end
      
    end
  end
end